provider "alicloud" {
  profile = "default"
  version = "1.153.0"
}

data "alicloud_zones" "all" {
}

module "vpc" {
  source = "../../2-vpc"

  vpc_name        = "ebike"
  vpc_cidr        = "192.168.0.0/16"
  vpc_description = "测试vpc"
  vpc_tags = {
    created = "Terraform"
  }

  vswitch_name       = "ebike"
  availability_zones = data.alicloud_zones.all.ids
  vswitch_cidrs      = [for i, v in data.alicloud_zones.all.ids : format("192.168.%d.0/24", i + 1)]
  vswitch_tags = {
    created = "Terraform"
  }
}

module "security" {
  source     = "../../3-security"
  name       = "default"
  vpc_id     = module.vpc.vpc_id
  key_pair_name = "tf-default"
  public_key_file = file("${path.module}/public.pem")
}

module "ecs_cluster" {
  source = "../../5-ecs"

  number_of_instances         = 3

  name                        = "ebike"
  host_name                   = "ebike"
  exist_azs                   = module.vpc.availability_zones
  exist_vswitch_ids           = module.vpc.vswitch_ids
  //  instance_type               = ""
  security_groups             = module.security.security_group_ids
  user_data                   = file("${path.module}/user-data.yaml")
  key_name                    = module.security.key_name
  use_num_suffix              = true
  system_disk_category        = "cloud_efficiency"
  internet_max_bandwidth_out  = 1
  associate_public_ip_address = true
  private_key                 = file("${path.module}/id_dsa")
  content                     = file("${path.module}/pipeline.sql")

  setup_db                    = false
}

module "slb" {
  source = "../"

  exist_azs = module.vpc.availability_zones
  exist_vswitch_ids = module.vpc.vswitch_ids

  instance_ids = module.ecs_cluster.instance_id
  virtual_server_group = [
    {
      name: "ebike"
      virtual_server: [
        {server_ids = 1, port = 8080},
        {server_ids = 1, port = 8081},
        {server_ids = 2, port = 8082}
      ]
    },
    {
      name: "ebike-high"
      virtual_server: [
        {server_ids = 1, port = 80},
        {server_ids = 2, port = 8087}
      ]
    },
    {
      name: "worker"
      virtual_server: [
        {server_ids = 1, port = 80},
        {server_ids = 2, port = 8087}
      ]
    },
    {
      name: "mbserver"
      virtual_server: [
        {server_ids = 1, port = 80},
        {server_ids = 2, port = 8087}
      ]
    },
    {
      name: "device"
      virtual_server: [
        {server_ids = 1, port = 9880}
      ]
    }
  ]

}